package com.ball.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ball.entity.MessageReply;
import com.ball.entity.UserInfo;
import com.ball.service.UserInfoService;





/**
 * @author zhuhaibin
 *
 */
@Controller
@RequestMapping("/user")
public class UserController {
	
	@Resource
	private UserInfoService userService;
	
	@RequestMapping("/showUser")
	public String toIndex(HttpServletRequest request,Model model) {
		int id = Integer.parseInt(request.getParameter("id"));  
        UserInfo user = userService.getUserById(id);  
        model.addAttribute("message", user.getUserName());  
        return "Helloworld";  
	}
	@RequestMapping("/login")
	@ResponseBody
	public MessageReply login(HttpServletRequest request){
		String userName = request.getParameter("userName");
		String password = request.getParameter("password");		
		UserInfo user = userService.login(userName,password);
		MessageReply msg = new MessageReply(); 
		if(user == null){
			msg.setStatus("failed");
			msg.setMessage("用户名或者密码错误"); 			
		}else{
			request.getSession().setAttribute("sessionUser", user);
			msg.setStatus("success");			
		}		
		return msg;
	}
	@RequestMapping("/index")
	public String loginSuccess(HttpServletRequest request,Model model){
		return "index";
	}
}
